<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/21
 * Time: 15:11
 */

namespace app\api\controller;


use app\common\controller\Base;
use think\Db;

class Member extends Base
{

//    /**
//     * 选择身份接口
//     * */
//    public function choseRole()
//    {
//        $role_id = input('role_id');
//
//        if(empty($role_id)){
//            $this->result('','-1','请选择您的身份','json',[]);
//        }
//        $userInfo = $this->check(input('token'));
//
//        $data['role_id'] = $role_id;
//        $res = Db::name('member')->where(['id' =>$userInfo->uid])->update($data);
//
//        if($res){
//            $this->result('','1','选择成功','json',[]);
//        }else{
//            $this->result('','-1','操作失败','json',[]);
//        }
//
//    }



    /**
     * 申请志愿者页面
     * */
    public function volunteer()
    {

        $political = Db::name('political')->where(['pid' => 0])->field('id,value')->select();
        foreach ($political as $k => $v){
            $politicalInfo = Db::name('political')->where(['pid' => $v['id']])->field('id,value')->select();
            $political[$k]['info'] = $politicalInfo;
        }
        $zhibu = Db::name('zhibu')->select();

        $data = [
            'political' => $political,
            'zhibu' => $zhibu
        ];

        $this->result($data,'1','获取数据','json');

    }


    /**
     * 申请成为志愿者
     * */
    public function applyVolunteer()
    {

        $token = input('token');
        $userInfo = $this->check($token);
        if($userInfo['role_id'] == 3){
            $this->result('','-1','您已经是志愿者','json');
        }

        $map[] = ['user_id','=',$userInfo['uid']];
        $map[] = ['status','<>',-1];
        $isVolunteer = Db::name('volunteer')->where($map)->count();

        $maps[] = ['user_id','=',$userInfo['uid']];
        $maps[] = ['status','=',-1];
        $isRe = Db::name('volunteer')->where($maps)->count();

        if($isVolunteer){
            $this->result('','-1','申请失败.请勿重复申请','json');
        }

        $name = input('name');
        $education = input('education');
        $born_date = input('born_date');
        $sex = input('sex');
        $mobile = input('mobile');
        $political = input('political');
        $political_info = input('political_info','无');
        if(!$name || !$born_date || !$sex || !$mobile || !$political){
            $this->result('','-1','数据填写不完整','json');
        }

        $zhibu = input('zhibu','0');
        $rudang_date = input('rudang_date','');
        $danwei = input('danwei','');
        $personal = input('personal','');
        $service = input('service','');


        if($isRe){
            $dataS['status'] = 0;
            $res = Db::name('volunteer')->where(['user_id' => $userInfo['uid']])->update($dataS);
        }else{

            $data = [
                'name' => $name,
                'education' => $education,
                'born_date' => $born_date,
                'sex' => $sex,
                'mobile' => $mobile,
                'political' => $political,
                'political_info' => $political_info,
                'zhibu' => $zhibu,
                'rudang_date' => $rudang_date,
                'danwei' => $danwei,
                'user_id' => $userInfo['uid'],
                'personal' => $personal,
                'service' => $service,
                'create_time' => time(),
                'status' => 0
            ];

            $res = Db::name('volunteer')->insert($data);
        }

        if($res){
            $this->result('','1','申请成功,请耐心等待','json');
        }else{
            $this->result('','-1','申请失败','json');
        }

    }


    /**
     * 会员中心页面
     * */
    public function memberInfo()
    {

        $token = input('token');

        $userInfo = $this->check($token);

        $member = Db::name('member')->where(['id' => $userInfo['uid']])->find();

        $map[] = ['user_id','=',$userInfo['uid']];
        $map[] = ['is_check','=','0'];
        $msgNum = Db::name('send_msg')->where($map)->count();

        $data = [
            'user_id' => $userInfo['uid'],
            'username' => $member['username'],
            'mobile' => $member['mobile'],
            'role_id' => $member['role_id'],
            'msg_num' => $msgNum
        ];

        $this->result($data,'1','获取成功','json');

    }


    /**
     * 会员页我得捐助申请
     * */
    public function helpList()
    {

        $token = input('token');
        $status = input('status','0');
        $userInfo = $this->check($token);

        $page = input('page');
        $limit = input('limit');

        $where[] = ['user_id|help_id','=',$userInfo['uid']];

        if($status == 0 || !$status){
        }else if($status == 1){
            $where[] = ['status','=','0'];
        }else if($status == 2){
            $where[] = ['status','=','1'];
        }else if($status == 3){
            $where[] = ['status','=','2'];
        }else if($status == 4){
            $where[] = ['status','between',[3,4]];
        }

        $list = Db::name('love_list')
            ->where($where)
            ->page($page,$limit)
            ->field('id,create_time,status,info,help_info,type,need_type,user_id,help_id')
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->withAttr('need_type', function ($value, $data) {
                if($value == 1){
                    return "物资需求";
                }else if($value == 2){
                    return "问答需求";
                }
            })
            ->withAttr('status', function ($value, $data) {
                if($value == 0){
                    return "未审核";
                }else if($value == 1){
                    return "未认领";
                }else if($value == 2){
                    return "已认领";
                }else if($value ==3){
                    return "已完成";
                }else if($value == 4){
                    return "已完成";
                }else if($value == -1){
                    return "已拒绝";
                }
            })
            ->select();

        foreach ($list as $k => $v){
            if($v['type'] == 1){
                if($v['status'] == "已完成"){
                    $list[$k]['is_rl'] = 1;
                }else{
                    $list[$k]['is_rl'] = 0;
                }
                $list[$k]['info'] = $v['info'];
                if($v['help_id'] == $userInfo['uid']){
                    $list[$k]['is_rl'] = 0;
                }
            }else{
                $list[$k]['is_rl'] = 0;
                if($v['help_id'] == $userInfo['uid'] && $v['status'] == "已完成"){
                    $list[$k]['is_rl'] = 1;
                }
                $list[$k]['info'] = $v['help_info'];
            }

            unset($list[$k]['help_info']);
        }

        $total = Db::name('love_list')->where($where)->count();

        $data = [
            'data' => $list,
            'total' => $total
        ];

        $this->result($data,'1','获取成功','json');

    }

    /**
     * 我得收藏
     * */
    public function myCollection()
    {

        $token = input('token');
        $userInfo = $this->check($token);
        $page = input('page');
        $limit = input('limit');

        $where['user_id'] = $userInfo['uid'];
        $where['status'] = 1;

        $order = 'id desc';
        $httpType = is_https();

        if($httpType){
            $https = "https://";
        }else{
            $https = "http://";
        }
        $http = $_SERVER['SERVER_NAME'];
        $list = Db::name('collection_work')
//            ->view('work_list','id,title,wages,company,create_time','work_list.id = collection_work.work_id','right')
            ->where($where)
            ->page($page,$limit)
            ->order($order)
            ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
            ->select();

        foreach ($list as $k => $v){
            $id = $v['work_id'];
            $count = Db::name('work_list')->where(['id' => $id])->count();
            if($count){
                $info = Db::name('work_list')->where(['id' => $id])->field('id,title,wages,company,create_time')
                    ->withAttr('create_time', function ($value, $data) {
                        if($value){
                            return date('Y-m-d H:i:s', $value);
                        }else{
                            return "--";
                        }
                    })
                    ->find();
                $list[$k]['title'] =$info['title'];
                $list[$k]['title'] =$info['title'];
                $list[$k]['wages'] =$info['wages'];
                $list[$k]['company'] =$info['company'];
                $list[$k]['create_time'] =$info['create_time'];
                $list[$k]['url'] = $https.$http."/index/index/work?id=".$id;
            }else{
                unset($list[$k]);
            }

        }
        $total =  Db::name('collection_work')->where($where)->count();

        $data = [
            'list' => array_values($list),
            'total' => $total
        ];

        $this->result($data,'1','获取成功','json');

    }


    /**
     * 我得帮扶
     * */
    public function myHelp()
    {

        $token = input('token');
        $userInfo = $this->check($token);
        $status = input('status',0);
        $where = [];

        $page = input('page');
        $limit = input('limit');

        $where[] = ['user_id','=',$userInfo['uid']];
        if(!$status || $status == 0){

        }else if($status == 1){
            $where[] = ['status','between',[1,2]];
        }else if($status == 2){
            $where[] = ['status','=',3];
        }else if($status == 3){
            $where[] = ['status','=',4];
        }
        $order = 'level desc && create_time desc';

        $list = Db::name('help_list')
            ->where($where)
            ->page($page,$limit)
            ->order($order)
            ->field('id,status,name,beizhu')
            ->select();

        foreach ($list as $k => $v){
            if($v['status'] == 0){
                $list[$k]['beizhu'] = "帮扶申请正在审核中";
            }else if($v['status'] == 1 || $v['status'] == 2){
                $list[$k]['beizhu'] = "帮扶正在帮助中";
            }else{
                $list[$k]['beizhu'] = $v['beizhu'];
            }
            if($v['status'] == 1){
                $list[$k]['status'] =  "待帮扶";
                $list[$k]['is_edit'] = 1;
            }else if($v['status'] == 2){
                $list[$k]['status'] =  "待帮扶";
                $list[$k]['is_edit'] = 1;
            }else if($v['status'] == 3){
                $list[$k]['status'] =  "帮扶中";
                $list[$k]['is_edit'] = 0;
            }else if($v['status'] == 4){
                $list[$k]['status'] =  "已帮扶";
                $list[$k]['is_edit'] = 0;
            }else if($v['status'] == 0){
                $list[$k]['status'] =  "未审核";
                $list[$k]['is_edit'] = 1;
            }else if($v['status'] == -1){
                $list[$k]['status'] =  "已拒绝";
                $list[$k]['is_edit'] = 1;
            }
        }

        $total =  Db::name('help_list')
            ->where($where)->count();

        $data = [
            'list' => $list,
            'total' => $total
        ];

        $this->result($data,'1','获取成功','json');

    }


    /**
     * 申请帮扶详情
     * */
    public function getHelpList()
    {

        $token = input('token');
        $status = input('status','0');
        $userInfo = $this->check($token);
        $where = [];
        $where[] = ['help_list.help_id','=',$userInfo['uid']];
//        $where[] = ['help_list.status','in',[-1,1,2,3]];

        if($status == 1){
            $where[] = ['help_list.status','=','1'];
        }else if($status == 2){
            $where[] = ['help_list.status','=','2'];
        }else if($status == 3){
            $where[] = ['help_list.status','=','3'];
        }

        $page = input('page');
        $limit = input('limit');
        $order = 'help_list.level desc,create_time desc';

        $list = Db::view('help_list','id,status,name,nation,sex,create_time,pass_msg,help_id,pass_time,beizhu,work_address')
            ->view('member','realname','help_list.help_id = member.id','left')
            ->where($where)
            ->page($page,$limit)
            ->order($order)
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d', $value);
                }else{
                    return "--";
                }
            })
            ->withAttr('pass_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d', $value);
                }else{
                    return "--";
                }
            })
            ->withAttr('sex', function ($value, $data) {
                if($value == 1){
                    return "男";
                }else if($value == 2){
                    return "女";
                }else{
                    return $value;
                }
            })
            ->withAttr('work_address', function ($value, $data) {
                $isB = strstr($value, '-');

                if(!$isB){

                    $arr = explode("区",$value);
                    if(isset($arr[1]) && !empty($arr[1])){
                        $arrs = explode('镇',$arr[1]);
                        $type = "镇";
                        if(count($arrs) < 2){
                            $arrs = explode('道',$arr[1]);
                            $type = "道";
                        }
                        if(count($arrs) < 2){
                            $arrs = explode('关',$arr[1]);
                            $type = "关";
                        }
                        if(count($arrs) < 2){
                            $arrs = explode(')',$arr[1]);
                            $type = ")";
                        }
                        if(count($arrs) < 2){
                            $arrs = explode('员',$arr[1]);
                            $type = "员";
                        }
                        if(isset($arrs[1]) && !empty($arrs[1])){
                            $res = $arr[0]."区-".$arrs[0].$type."-".$arrs[1];
                        }else{
                            $res = $arr[0]."区-".$arr[1];
                        }
                    }else{
                        $res = $value;
                    }

                    return $res;

                }else{
                    return $value;
                }
            })
            ->select();

        foreach ($list as $k => $v){

            if($v['status'] == 1){
                $list[$k]['status_name'] = "待帮扶";
            }else if($v['status'] == 2){
                $list[$k]['status_name'] =  "帮扶中";
            }else if($v['status'] == 3){
                $list[$k]['status_name'] =  "已帮扶";
                $list[$k]['pass_msg'] = $v['beizhu'];
            }else if($v['status'] == 0){
                $list[$k]['status_name'] =  "未帮扶";
            }else if($v['status'] == -1){
                $list[$k]['status_name'] =  "已拒绝";
            }
            $list[$k]['type'] = "援助申请";
            if($v['status'] == 1 || $v['status'] == -1 || $v['status'] == 0){
                $list[$k]['is_edit'] = 1;
            }else{
                $list[$k]['is_edit'] = 0;
            }
            unset($list[$k]['beizhu']);
        }

        $total =  Db::view('help_list','id,status,name,nation,sex,type,finish_time,pass_msg,help_id')
            ->view('member','realname','help_list.help_id = member.id','left')
            ->where($where)->count();

        $data = [
            'list' => $list,
            'total' => $total
        ];

        $this->result($data,'1','获取成功','json');

    }


    /**
     * 帮扶编辑，查看页面
     * */
    public function checks()
    {

        $id = input('id');
        if(!$id){
            $this->result('','-1','获取信息失败','json');
        }

        $token = input('token');

        $userInfo = $this->check($token);

        $roleId = $userInfo['role_id'];

//        if($roleId == 1){
//            $this->result('','-1','无权限查看','json');
//        }

        $where[] = ['id','=',$id];

        $order = 'level desc';
        $list = Db::name('help_list')
            ->where($where)
            ->order($order)
            ->field('id,level,type,name,type,msg,nation,sex,born_date,mobile,work_address,work_address_info,rudang_date,danwei,msg,msg_info,zhibu,id_img_up,id_img_down,buchong,status,id_code,cardname,is_dang')
            ->withAttr('born_date', function ($value, $data) {
                $arr = explode('/',$value);
                return implode('-',$arr);
            })
            ->withAttr('rudang_date', function ($value, $data) {
                $arr = explode('/',$value);
                return implode('-',$arr);
            })
            ->withAttr('level', function ($value, $data) {
                if($value == 1){
                    return "一般";
                }else{
                    return "紧急";
                }
            })
            ->withAttr('work_address', function ($value, $data) {
                $arr = explode ('区',$value);
                $arr2 = explode('镇',$arr[1]);
                return $arr[0]."区-".$arr2[0]."镇-".$arr2[1];
            })
            ->withAttr('sex', function ($value, $data) {
                if($value == 1){
                    return "男";
                }else if($value == 2){
                    return "女";
                }else{
                    return $value;
                }
            })
            ->withAttr('id_img_up', function ($value, $data) {
                $val = Db::name('images')->where(['id' => $value])->value('img');
                return $val;
            })
            ->withAttr('id_img_down', function ($value, $data) {
                $val = Db::name('images')->where(['id' => $value])->value('img');
                return $val;
            })
            ->withAttr('buchong', function ($value, $data) {
                $imgArr = [];
                if($value){
                    $arr = explode(',',$value);
                    foreach ($arr as $k => $v){
                        $val = Db::name('images')->where(['id' => $v])->value('img');
                        if($val){
                            array_push($imgArr,$val);
                        }
                    }
                }
                return $imgArr;
            })
            ->withAttr('is_dang', function ($value, $data) {
                if($value == 0){
                    return "否";
                }else{
                    return "是";
                }
            })
            ->find();
        $zhi = Db::name('zhibu')->select();
        $list['danwei_list'] = $zhi;

        $this->result($list,'1','获取成功','json');


    }


    /**
     * 帮扶编辑
     * */
    public function edit()
    {

        $id = input('id');
        $token = input('token');
        $userInfo = $this->check($token);
        if(!$id){
            $this->result('','-1','数据获取有误','json');
        }

        $data = input('post.');
        unset($data['id']);

        $old = Db::name('help_list')->where(['id' => $id])->find();
        if(in_array($old['status'],['-1','3'])){
            $this->result('','-1','当前帮扶状态不可编辑','json');
        }

        if($old['status'] == -1){
            $data['status'] = 0;
        }

        unset($old['id']);
        unset($old['help_id']);
        unset($old['status']);
        unset($old['pass_msg']);
        unset($old['create_time']);
        unset($old['pass_time']);
        unset($old['finish_time']);
        unset($old['beizhu']);
        unset($old['user_id']);
        unset($data['token']);

        if(isset($data['id_img_up'])){
            $idImg = [
                'img' => $data['id_img_up'],
                'type' => 3,
                'user_id' => $userInfo['uid'],
                'create_time' => time()
            ];
            $imgId = Db::name('images')->insertGetId($idImg);
            $data['id_img_up'] = $imgId;
        }

        if(isset($data['id_img_down'])){
            $idImgs = [
                'img' => $data['id_img_down'],
                'type' => 4,
                'user_id' => $userInfo['uid'],
                'create_time' => time()
            ];
            $imgIds = Db::name('images')->insertGetId($idImgs);
            $data['id_img_down'] = $imgIds;
        }
        if(isset($data['buchong'])){
            $imgIdB = "";
            $buchongArr = json_decode($data['buchong'],true);
            foreach ($buchongArr as $k => $v){
                $imgInfo = array(
                    'type' => 2,
                    'img' => $v,
                    'user_id' => $userInfo['uid'],
                    'create_time' => time()
                );
                $imgId = Db::name('images')->insertGetId($imgInfo);
                $imgIdB .= $imgId.",";
            }
            $imgIdB = substr($imgIdB,0,strlen($imgIdB)-1);
            $data['buchong'] = $imgIdB;
        }

        if(isset($data['rudang_date'])){
            $rDate = explode('/',$data['rudang_date']);
            $cr = count($rDate);
            if(!isset($rDate[1])){
                $data['rudang_date'] = "";
            }else{
                if($rDate[1]){
                    if(isset($rDate[2])){
                        if(!$rDate[2]){
                            $data['rudang_date'] = $rDate[0]."/".$rDate[1]."/1";
                        }
                    }else{
                        $data['rudang_date'] = $rDate[0]."/1/1";
                    }
                }else{
                    if($rDate[0]){
                        $data['rudang_date'] = $rDate[0]."/1/1";
                    }else{
                        $data['rudang_date'] = "";
                    }
                }
            }
        }

//        if(isset($data['id_code']) || isset($data['cardname'])){
//            $memberMap[] = ['card_id','=',$data['id_code']];
//            $memberMap[] = ['real_name','=',$data['cardname']];
//            $partyMember = Db::name('party_member')->where($memberMap)->count();
//            if($partyMember == 0){
//                $this->result('','-1','系统无该党员信息','json');
//            }
//        }

        $res = Db::name('help_list')->where(['id' => $id])->update($data);

        if($res || $res==0){
            $this->result('','1','修改成功','json');
        }else{
            $this->result('','-1','修改失败','json');
        }

    }



    /**
     * 帮扶审核
     *
     * */
    public function examine()
    {

        $id = input('id');
        $type = input('type');
        $role_bei = input('role_bei');
        $token = input('token');
        $user = $this->check($token);

        if(!$id || !$type || !$role_bei){
            $this->result('','-1','数据获取失败','json');
        }

        $zhi = Db::name('help_list')->where(['id'=>$id])->field('zhibu,level,user_id,pass_user,status,danwei')->find();

        if($type == 1){
            if($zhi['level'] == 1){
                if($zhi['status'] == 0){
                    $data['status'] = 1;
                }else if($zhi['status'] == 1){
                    $data['status'] = 2;
                }else{
                    $data['status'] = 3;
                }
            }else{
                if($user['uid'] == $zhi['pass_user']){
                    $data['status'] = 0;
                }else{
                    $data['status'] = 2;
                }
            }
            $msg = "审核";
        }else{
            $data['status'] = -1;
            $msg = "拒绝";
        }
        $data['pass_time'] = time();
        $data['pass_user'] = $user['uid'];
        $res = Db::name('help_list')->where(['id' => $id])->update($data);

        $userArr = [];
        if($res || $res === 0){

            if($zhi['level'] == 2){

                if(!empty($zhi['zhibu'])){
                    $zCount = Db::name('help_list')->where(['zhibu'=>$zhi['zhibu']])->count();
                    if($zCount > 0){
                        $memberMap[] = ['zhibu','=',$zhi['zhibu']];
                    }else{
                        $memberMap[] = ['danwei','=',$zhi['danwei']];
                    }
                }
                $memberMap[] = ['role_id','=',2];

                $memberList = Db::name('member')->where($memberMap)->field('id')->select();
                $memberArr = [];

                foreach ($memberList as $k=>$v){
                    array_push($memberArr,$v['id']);
                }
                if(count($memberArr) > 0){
                    $key = array_rand($memberArr,1);
                    $helpId = $memberArr[$key];
                    array_push($userArr,$helpId);
                }else{
                    $helpId = 0;
                }
                $updateHelp['help_id'] = $helpId;
                $updateHelp['pass_user'] = $helpId;
                Db::name('help_list')->where(['id' => $id])->update($updateHelp);
            }else{

                if($zhi['status'] < 2){
                    $zCount = Db::name('member')->where(['zhibu'=>$zhi['zhibu']])->count();
                    if($zCount){
                        $memberMap[] = ['zhibu','=',$zhi['zhibu']];
                    }else{
                        $memberMap[] = ['danwei','=',$zhi['danwei']];
                    }
                    $memberMap[] = ['role_id','=',2];
                    $memberList = Db::name('member')->where($memberMap)->field('id')->select();
                    $memberArr = [];
                    foreach ($memberList as $k=>$v){
                        array_push($memberArr,$v['id']);
                    }
                    if(count($memberArr) > 0){
                        $key = array_rand($memberArr,1);
                        $helpId = $memberArr[$key];
                        array_push($userArr,$helpId);
                    }else{
                        $helpId = 0;
                    }

                    $updateHelp['help_id'] = $helpId;
                    $updateHelp['pass_user'] = $helpId;
                    Db::name('help_list')->where(['id' => $id])->update($updateHelp);
                }

            }

            $msgMap[] = ['user_id','=',$zhi['user_id']];
            $msgMap[] = ['apply_id','=',$id];
            $msgC = Db::name('send_msg')->where($msgMap)->find();
            if(count($msgC)){
                if($msgC['is_check'] == 1){
                    $msgData['is_check'] = 0;
                    $msgData['content'] = $role_bei;
                    Db::name('send_msg')->where(['id' => $id])->update($msgData);
                }
            }else{
                $msgData = [
                    'user_id' => $zhi['user_id'],
                    'apply_id' => $id,
                    'title' => "您的帮扶申请有了新的进度",
                    'content' => $role_bei,
                    'create_time' => time(),
                    'is_check' => 0
                ];
                Db::name('send_msg')->insert($msgData);
            }

            $jPush = new Jpushmember();
            array_push($userArr,$zhi['user_id']);
            $content = "您的申请有了新的进度";
            $res = $jPush->pushAlias($userArr,$content);
            //查询是否由该条信息


            $this->result('','1',$msg.'成功','json');
        }else{
            $this->result('','-1',$msg.'失败','json');
        }

    }


    /**
     * 消息列表
     * */
    public function msgList()
    {

        $token = input('token');
        $userInfo = $this->check($token);
        $page = input('page');
        $limit = input('limit');

        $uid = $userInfo['uid'];

        $where[] = ['send_msg.user_id','=',$uid];

        $map[] = ['user_id','=',$uid];
        //$map[] = ['is_check','=','0'];
       
        $order = "is_check desc , create_time desc";

        $list = Db::view('send_msg','apply_id,title,content,create_time,type')
            ->view('help_list','status,buchong','send_msg.apply_id = help_list.id','left')
            ->where($where)
            ->page($page,$limit)
            ->order($order)
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d', $value);
                }else{
                    return "--";
                }
            })
            ->withAttr('buchong', function ($value, $data) {
                if($value){
                    return $value;
                }else{
                    return "--";
                }
            })
            ->withAttr('status', function ($value, $data) {
                if($value == 1){
                    return "待帮扶";
                }else if($value == 0){
                    return "未审核";
                }else if($value == 3){
                    return "已帮扶";
                }else if($value == 2){
                    return "待帮扶";
                }else if($value == -1){
                    return "未帮扶";
                }
            })
            ->withAttr('buchong', function ($value, $data) {
                if(!$value){
                    return "";
                }
            })
            ->select();
        $httpType = is_https();

        if($httpType){
            $https = "https://";
        }else{
            $https = "http://";
        }

        $http = $_SERVER['SERVER_NAME'];
        foreach ($list as $k => $v){
            if($v['buchong']){
                $arr = explode(',',$v['buchong']);
                $a = count($arr);
                $pic = Db::name('images')->where(['id' => $arr[$a-1]])->field('type,img')->find();
                if($pic['type'] == 5){
                    $img = $https.$http."/upolods/images/helpArticle/".$pic['img'];
                }else{
                    $img = $https.$http.$pic['img'];
                }
                $list[$k]['pic'] = $img;
            }else{
                $list[$k]['pic'] = "";
            }
        }

        $total =  Db::view('send_msg','apply_id,title,content,create_time')
            ->view('help_list','status,buchong','send_msg.apply_id = help_list.id')
            ->where($where)->count();

        $upDate['is_check'] = 1;
        $res = Db::name('send_msg')->where($map)->update($upDate);

        $data = [
            'list' => $list,
            'count' => $total
        ];

        $this->result($data,'1','获取成功','json');

    }


    /**
     *
     * 消息列表---消息详情
     *
     * */
    public function msgInfo()
    {

        $id = input('id');

        if(!$id){
            $this->result('','-1','参数获取有误','json');
        }

        $where[] = ['id','=',$id];
        $data = Db::name('send_msg')->where($where)
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d', $value);
                }else{
                    return "--";
                }
            })
            ->find();
        if($data['type'] == 1){
            $map[] = ['id','=',$data['apply_id']];
            $datas = Db::name('help_list')->where($map)->find();
        }else if($data['type'] == 2){
            $map[] = ['id','=',$data['apply_id']];
            $datas = Db::name('love_list')->where($map)->find();
        }else{
            $map[] = ['id','=',$data['apply_id']];
            $datas = Db::name('volunteer')->where($map)->find();
        }
        $datas['title'] = $data['title'];
        $datas['content'] = $data['content'];
        $datas['sendTime'] = $data['create_time'];

        $this->result($datas,'1','获取成功','json');

    }


    /**
     *
     * 新街口---获取申请帮扶,我的帮扶,我的捐助
     *
     * */
    public function getMyList()
    {

        $token = input('token');
        $type = input('type','1');
        $userInfo = $this->check($token);
        $where = [];
//        $where[] = ['help_list.status','in',[-1,1,2,3]];

        $page = input('page');
        $limit = input('limit');

        if($type == 1){

            $where[] = ['help_list.help_id','=',$userInfo['uid']];


            $order = 'help_list.level desc && create_time desc';

            $list = Db::view('help_list','id,status,name,nation,sex,create_time,pass_msg,help_id,pass_time,beizhu')
                ->view('member','realname','help_list.help_id = member.id','left')
                ->where($where)
                ->page($page,$limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d', $value);
                    }else{
                        return "--";
                    }
                })
                ->withAttr('pass_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d', $value);
                    }else{
                        return "--";
                    }
                })
                ->withAttr('sex', function ($value, $data) {
                    if($value == 1){
                        return "男";
                    }else if($value == 2){
                        return "女";
                    }else{
                        return $value;
                    }
                })
                ->select();

            foreach ($list as $k => $v){
                if($v['status'] == 1){
                    $list[$k]['status_name'] = "待审核";
                }else if($v['status'] == 2){
                    $list[$k]['status_name'] =  "帮扶中";
                }else if($v['status'] == 3){
                    $list[$k]['status_name'] =  "帮扶中";
                }else if($v['status'] == 4){
                    $list[$k]['status_name'] =  "已帮扶";
                    $list[$k]['pass_msg'] = $v['beizhu'];
                }else if($v['status'] == 0){
                    $list[$k]['status_name'] =  "未帮扶";
                }else if($v['status'] == -1){
                    $list[$k]['status_name'] =  "未帮扶";
                }
                $list[$k]['type'] = "援助申请";
                if($v['status'] == 1 ||$v['status'] == 0||$v['status'] == -1){
                    $list[$k]['is_edit'] = 1;
                }else{
                    $list[$k]['is_edit'] = 0;
                }
                unset($list[$k]['beizhu']);
            }

            $total =  Db::view('help_list','id,status,name,nation,sex,type,finish_time,pass_msg,help_id')
                ->view('member','realname','help_list.help_id = member.id','left')
                ->where($where)->count();

        }else if($type == 2){


            $where[] = ['user_id','=',$userInfo['uid']];
            $where[] = ['status','>=',0];
            $order = 'level desc && create_time desc';

            $list = Db::name('help_list')
                ->where($where)
                ->page($page,$limit)
                ->order($order)
                ->field('id,status,name,beizhu')
                ->select();

            foreach ($list as $k => $v){
                if($v['status'] == 0){
                    $list[$k]['beizhu'] = "帮扶申请正在审核中";
                }else if($v['status'] == 1 || $v['status'] == 2){
                    $list[$k]['beizhu'] = "帮扶正在帮助中";
                }else{
                    $list[$k]['beizhu'] = $v['beizhu'];
                }
                if($v['status'] == 1){
                    $list[$k]['status'] =  "未审核";
                    $list[$k]['is_edit'] = 1;
                }else if($v['status'] == 2){
                    $list[$k]['status'] =  "待帮扶";
                    $list[$k]['is_edit'] = 1;
                }else if($v['status'] == 3){
                    $list[$k]['status'] =  "帮扶中";
                    $list[$k]['is_edit'] = 0;
                }else if($v['status'] == 4){
                    $list[$k]['status'] =  "已帮扶";
                    $list[$k]['is_edit'] = 0;
                }else if($v['status'] == 0){
                    $list[$k]['status'] =  "未审核";
                    $list[$k]['is_edit'] = 1;
                }
            }

            $total =  Db::name('help_list')
                ->where($where)->count();

        }else{


            $where[] = ['user_id|help_id','=',$userInfo['uid']];

            $list = Db::name('love_list')
                ->where($where)
                ->page($page,$limit)
                ->field('id,create_time,status,info,help_info,type,need_type,user_id,help_id')
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->withAttr('need_type', function ($value, $data) {
                    if($value == 1){
                        return "物资需求";
                    }else if($value == 2){
                        return "问答需求";
                    }
                })
                ->withAttr('status', function ($value, $data) {
                    if($value == 0){
                        return "未审核";
                    }else if($value == 1){
                        return "未认领";
                    }else if($value == 2){
                        return "已认领";
                    }else if($value ==3){
                        return "已完成";
                    }else if($value == 4){
                        return "已完成";
                    }else if($value == -1){
                        return "已拒绝";
                    }
                })
                ->select();

            foreach ($list as $k => $v){
                if($v['type'] == 1){
                    $list[$k]['is_rl'] = 1;
                    $list[$k]['info'] = $v['info'];
                    if($v['help_id'] == $userInfo['uid']){
                        $list[$k]['is_rl'] = 0;
                    }
                }else{
                    $list[$k]['is_rl'] = 0;
                    if($v['help_id'] == $userInfo['uid']){
                        $list[$k]['is_rl'] = 1;
                    }
                    $list[$k]['info'] = $v['help_info'];
                }

                unset($list[$k]['help_info']);
            }

            $total = Db::name('love_list')->where($where)->count();

        }

        $data = [
            'list' => $list,
            'total' => $total
        ];

        $this->result($data,'1','获取成功','json');

    }


    public function volunteerInfo()
    {

        $id = input('id','');
        if(!$id){
            $this->result('','-1','参数获取有误','json');
        }

        $data = Db::name('volunteer')->where(['id' => $id])
            ->withAttr('political', function ($value, $data) {
                $value = Db::name('political')->where(['id' => $value])->value('value');
                if(!$value){
                    $value = "";
                }
                return $value;
            })
            ->withAttr('political_info', function ($value, $data) {
                $value = Db::name('political')->where(['id' => $value])->value('value');
                if(!$value){
                    $value = "";
                }
                return $value;
            })
            ->withAttr('zhibu', function ($value, $data) {
                $value =  Db::name('zhibu')->where(['id' => $value])->value('value');
                if(!$value){
                    $value = "";
                }
                return $value;
            })
            ->withAttr('beizhu', function ($value, $data) {
                if(!$value){
                    $value = "";
                }
                return $value;
            })
            ->find();

        $this->result($data,'1','获取成功','json');

    }


}